/**
 * 仓库页面
 */
class wareHouse extends eui.Component{
    private return:eui.Button;
    private seed:eui.Image;
    private harvest:eui.Image;
    private btn:eui.Button;

    private seedScroll:eui.Scroller;
    private seedList:eui.List;

    private _arrayData:eui.ArrayCollection;
    private _seedData:Array<any> = [];
    private _harvestData:Array<any> = [];
    private _page:string = "seed";
    private _exGoodsUI:exGoods;
    private _toast:Toast = new Toast();
    public constructor(){
        super();
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "wareHouseSkin"; 
    }

    private createCompleteEvent():void{
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        this.inIt();
    }

    private inIt(){
        this.return.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{this.dispatchEventWith("return")},this);
        this.seed.addEventListener(egret.TouchEvent.TOUCH_TAP,this.changeGoods,this);
        this.harvest.addEventListener(egret.TouchEvent.TOUCH_TAP,this.changeGoods,this);
        this.btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.changeGoods,this);

        Dispatcher.ins.addEventListener("wareHouseList",this.assignValue,this);
        Dispatcher.ins.addEventListener("plantBotany",this.loopPlant,this);

        this._arrayData =  new eui.ArrayCollection(this._seedData);
        this.seedList.dataProvider = this._arrayData;
        this.seedList.itemRenderer = wareHouseIR;
        this.getList();
    }
    private changeGoods(evt:egret.TouchEvent):void{
        let btn = evt.currentTarget;
        switch (btn){
            case this.seed:{
                 this.seed.source = "icon_seed_on_png";
                 this.harvest.source = "icon_harvest_off_png";
                 this._page = "seed";
                 this.btn.label = "去播种";
                 if(this._seedData.length == 0){
                     this.getList();
                 }
                 this._arrayData =  new eui.ArrayCollection(this._seedData);
                 this.seedList.dataProvider = this._arrayData;
                 this._arrayData.refresh();
                 break;
            }
            case this.harvest:{
                 this.seed.source = "icon_seed_off_png";
                 this.harvest.source = "icon_harvest_on_png";
                 this._page = "harvest";
                 this.btn.label = "去发货";
                 if(this._harvestData.length == 0){
                     this.getList();
                 }
                 this._arrayData =  new eui.ArrayCollection(this._harvestData);
                 this.seedList.dataProvider = this._arrayData;
                 this._arrayData.refresh();
                 break;
            }
            case this.btn:{
                if(this._page == "seed"){
                    this.toPlantInit();
                }else if(this._page == "harvest"){
                    let arr = [];
                    let trArr = [];
                    arr = this._harvestData;
                    let num = 0;
                    let type = 0;
                    for(let i = 0,lg = arr.length;i < lg;i ++){
                        let obj = arr[i];
                        if(obj.num.substr(0,1) == "X"){
                            obj.num = obj.num.substr(1);
                        }
                        if(obj.num > 0){
                            num = num + parseInt(obj.num);
                            obj.num = "X" + obj.num;
                            trArr.push(obj);
                        }
                    }
                    if(num == 0){
                        this._toast.show("请选择兑换的商品");
                        this.addChild(this._toast);
                        return;
                    }
                    for(let i = 0,lg = trArr.length;i < lg;i ++){
                        if(parseInt(trArr[i].SType) == 0 && trArr.length > 1){
                            this._toast.show("鲜花不可以混合兑换哦~")
                            this.addChild(this._toast);
                            return;
                        }
                    }
                    if(trArr[0].SType == 0 && num < 12){
                        this._toast.show("鲜花需要12份才能包邮哦~");
                        this.addChild(this._toast);
                        return;
                    }
                    if(trArr[0].SType == 1 && num < 5){
                        this._toast.show("多肉需要5份才能包邮哦~");
                        this.addChild(this._toast);
                        return;
                    }
                    if(!this._exGoodsUI){
                        this._exGoodsUI = new exGoods(trArr);
                        this._exGoodsUI.addEventListener("ok",()=>{
                            this._toast.show("兑换成功，期待你的小可爱来到身边吧！");
                            this.addChild(this._toast);
                            this.getList();   
                        },this);
                    }else{
                        this._exGoodsUI.valueGoods(trArr);
                    }
                    this.addChild(this._exGoodsUI);
                }
            }
        }
    }
    private _lantPosition:Array<any>;
    private _toPlant:Array<any>;
    private toPlantInit():void{
        let num = 0;
        if(this._seedData.length == 0){
            this._toast.show("请先至商店购买种子进行种植");
            this.addChild(this._toast);       
            return;     
        }
        for(let i = 0,lg = this._seedData.length;i < lg;i++){
            if( this._seedData[i].num > 0){
                num = num + this._seedData[i].num;
            }
        }
        if(num == 0){
            this._toast.show("选择种子进行播种");
            this.addChild(this._toast);   
            return;
        }
        this._lantPosition = [];
        for(let i = 0;i < 9;i++){
            if(common.plantIndex[i] == 0){
                this._lantPosition.push(i);//土地索引id
            }
        }
        if(this._lantPosition.length == 0){
            this._toast.show("已经没有多余的土地可以进行播种了哦");
            this.addChild(this._toast);   
            return;           
        }
        this.toPlant(0);
    }
    public toPlant(i,plant:string=""):void{
        if(plant == "ok"){
            this._lantPosition.shift();
            this._seedData[i].num = this._seedData[i].num - 1;
        }
        if(this._seedData[i].num > 0){
            let obj = this._seedData[i];
            if(this._lantPosition.length > 0){
                let dataPost = {
                    "indexId":this._lantPosition[0],
                    "dId":this._seedData[i].Id,
                    "pId":this._seedData[i].PropId
                }
                let note = i;
                socket.request("plantBotany",dataPost,note);
            }else{
                this._toast.show("没有多余的种植土地了");
                this.addChild(this._toast);
                setTimeout(this.plantOver(),1000);
            }
        }else{
            if(this._seedData.length - 1 > i){
                this.toPlant(i+1);
            }else{
                this._toast.show("播种完成");
                this.addChild(this._toast);     
                setTimeout(this.plantOver(),1000);
            }
        }
    }
    public loopPlant(evt:GameEvent):void{
        let obj = evt.data;
        if(obj.note !== -1){
            this.toPlant(obj.note,"ok");
        }
    }
    public plantOver():void{
        this.dispatchEventWith("resetdraw");
        this.dispatchEventWith("return");
    }
    public getList():void{
        let dataPost:any = {};
        let type:number = 0;
        if(this._page == "seed"){
            type = 0;
        }else{
            type = 1;
        }   
        dataPost.type = type;
        socket.request("wareHouseList",dataPost,type);
    }
    public assignValue(evt:GameEvent):void{
        let obj = evt.data;
        let list = obj.list;
        let type = obj.note;
        let arr = [];
        for(let i = 0,lg = list.length;i < lg;i++){
            let trObj = list[i];
            if(type == 0){
                trObj.icon = "sh_"+ list[i].PropId+"_png";
            }else if(type == 1){
                trObj.icon = "harvest_"+ list[i].PropId+"_png";
            }
            trObj.num = "0";
            arr.push(trObj);
        }
        switch (obj.note){
            case 0:{
                this._seedData = arr;
                break;
            }
            case 1:{
                this._harvestData = arr;
                break;
            }
        }
        this._arrayData = new eui.ArrayCollection(arr);
        this.seedList.dataProvider = this._arrayData;
        this._arrayData.refresh();
    }
    public changePage(note):void{
        if(note == "harvest"){
            this.harvest.dispatchEventWith(egret.TouchEvent.TOUCH_TAP);
        }
    }
}
class wareHouseIR extends eui.ItemRenderer{
    private sign:eui.Rect;
    private add:eui.Rect;
    private num:eui.Label;
    private _num:number = 0;
    private addYY:eui.Image;
    private signYY:eui.Image;
    public constructor(){
        super();
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "wareHouseIRSkin";
    }
    private createCompleteEvent():void{
        super.createChildren();
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        this.sign.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            if(this._num > 0){
                this._num = this._num - 1;
            }
            this.num.text = this._num + "";
            this.dataChanged();
        },this);
        this.sign.addEventListener(egret.TouchEvent.TOUCH_BEGIN,()=>{
            if(this._num == 0) return;
            this.signYY.visible = true;
        },this);
        this.sign.addEventListener(egret.TouchEvent.TOUCH_END,()=>{
            this.signYY.visible = false;
        },this);
        this.sign.addEventListener(egret.TouchEvent.TOUCH_MOVE,()=>{
            this.signYY.visible = false;
        },this);
        this.add.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            if(this._num < this.data.PropNum){
                this._num ++;
            }
            this.num.text = this._num + "";
            this.dataChanged();
        },this);     
        this.add.addEventListener(egret.TouchEvent.TOUCH_BEGIN,()=>{
            if(this._num == this.data.PropNum) return;
            this.addYY.visible = true;
        },this);
        this.add.addEventListener(egret.TouchEvent.TOUCH_END,()=>{
            this.addYY.visible = false;
        },this);
        this.add.addEventListener(egret.TouchEvent.TOUCH_MOVE,()=>{
            this.addYY.visible = false;
        },this);
    }
    protected dataChanged():void{
        this.data.num = this._num + "";
    }
} 